package com.gcstorage.tvdev.net;

import android.content.Intent;

import androidx.annotation.NonNull;

import com.alibaba.android.arouter.launcher.ARouter;
import com.alibaba.fastjson.JSON;

import com.blankj.utilcode.util.LogUtils;
import com.blankj.utilcode.util.ToastUtils;
import com.gcstorage.tvdev.util.SpUtils;
import com.google.gson.JsonElement;
import com.google.gson.JsonParseException;

import org.json.JSONException;

import java.net.ConnectException;
import java.net.SocketTimeoutException;
import java.text.ParseException;

import io.reactivex.Observer;
import io.reactivex.disposables.Disposable;
import retrofit2.HttpException;

/**
 * Created by Xu on 2019-10-29 14:33
 */
public abstract class RxObserver implements Observer<JsonElement> {

    @Override
    public void onSubscribe(Disposable disposable) {
        onStart();
    }

    @Override
    public void onNext(JsonElement t) {
        onH5Success(t);
        HttpResult result = JSON.parseObject(t.toString(), HttpResult.class);
        if (result.getCode() == 401) {
            ARouter.getInstance().build("/tv/app/login")
                    .withFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK | Intent.FLAG_ACTIVITY_NEW_TASK)
                    .navigation();
            SpUtils.clear(SpUtils.TAG_USER);
            SpUtils.putBoolean(SpUtils.TAG_USER, "user_login_status", false);
        } else if (result.getCode() == 0 || result.getCode() == 200 || result.getCode() == 20000) {
            if (result.getData() == null) {
                ToastUtils.showShort("接口数据结果为空");
                return;
            }
            if (!result.getData().toString().contains("{") && !result.getData().toString().contains("[")) {
                ToastUtils.showShort(result.getData().toString());
            }
            onSuccess(t);
        } else {
            ToastUtils.showShort(result.getMsg() == null ? "接口数据信息为空" : result.getMsg());
        }
    }

    @Override
    public void onError(@NonNull Throwable e) {
        String errorMsg = "";
        if (e instanceof HttpException) {//HTTP错误
            errorMsg = "网络错误:" + ((HttpException) e).code();
        } else if (e instanceof ServerException) {//服务器返回的错误
            errorMsg = "服务器错误";
        } else if (e instanceof JsonParseException || e instanceof JSONException || e instanceof ParseException) {
            errorMsg = "解析错误"; //均视为解析错误
        } else if (e instanceof ConnectException) {
            errorMsg = "连接失败";  //均视为网络错误
        } else if (e instanceof SocketTimeoutException) {
            errorMsg = "连接超时";
        } else if (e instanceof IllegalArgumentException) {
            errorMsg = "接口地址错误";
        } else if (e instanceof IllegalStateException) {
            errorMsg = "访问地址出现异常";
        } else {
            errorMsg = "接口返回异常";//未知错误
        }
        e.printStackTrace();
        LogUtils.d(" it $it----------------------------");
        onFail(NetConfig.ERROR_NET, errorMsg);
        onFinish();
        ToastUtils.showShort(e.getMessage() == null ? ((CustomException) e).msg : errorMsg);
    }

    @Override
    public void onComplete() {
        onFinish();
    }

    public abstract void onSuccess(JsonElement t);

    public void onH5Success(JsonElement t) {
    }

    public void onFail() {
    }

    public void onFail(int code, String msg) {
        onFail();
    }

    public void onFinish() {
    }

    public void onStart() {

    }
}
